package com.haotunet.app.goldtreasure.views;

import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.BitmapDrawable;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.view.ViewGroup;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.haotunet.app.goldtreasure.R;
import com.haotunet.app.goldtreasure.other.Interface.OnValueSetClickListener;

public class InvoicePopupView extends PopupWindow implements OnClickListener, OnTouchListener {
	private Activity mcontext;
	private LayoutInflater mLayoutInflater;
	private View rootView;
	private TextView mCancleBtn, tvNeed, tvUnwanted;

	private OnValueSetClickListener mListener;

	public InvoicePopupView(Activity context) {
		mcontext = context;
		mLayoutInflater = (LayoutInflater) mcontext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		initViews();
	}

	private void initViews() {
		rootView = mLayoutInflater.inflate(R.layout.invoice_popup_view, null);
		setContentView(rootView);
		mCancleBtn = (TextView) rootView.findViewById(R.id.invoice_cancle_btn);
		tvNeed = (TextView) rootView.findViewById(R.id.invoice_need_btn);
		tvUnwanted = (TextView) rootView.findViewById(R.id.invoice_unwanted_btn);
		setFocusable(true);
		setAnimationStyle(R.style.AnimBottom);
		setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
		setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
		setBackgroundDrawable(new BitmapDrawable());
		rootView.setOnTouchListener(this);
		mCancleBtn.setOnClickListener(this);
		tvUnwanted.setOnClickListener(this);
		tvNeed.setOnClickListener(this);
	}

	@Override
	public boolean onTouch(View v, MotionEvent event) {
		int height = rootView.findViewById(R.id.invoice_pop_layout).getTop();
		int y = (int) event.getY();
		if (event.getAction() == MotionEvent.ACTION_UP) {
			if (y < height) {
				dismiss();
			}
		}
		return true;
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.invoice_cancle_btn:
			dismiss();
			break;
		case R.id.invoice_need_btn:
			if (mListener != null) {
				mListener.onValueSet("要");
			}
			dismiss();
			break;
		case R.id.invoice_unwanted_btn:
			if (mListener != null) {
				mListener.onValueSet("不要");
			}
			dismiss();
			break;
		default:
			break;
		}
	}

	public void setValueSetClickListener(OnValueSetClickListener listener) {
		mListener = listener;
	}

}
